package com.x.controller;

import cn.hutool.core.thread.ThreadUtil;
import com.xxx.annotation.MyLogger;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;


/**
 * the class of advance
 *
 * <p>
 * .
 *
 * @author little_lunatic
 * @date 2024-07-25
 */
@Slf4j
//@RestController
public class RedisController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private Jedis jedis;

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("tmp")
    public String testRedisTmp() {
        BoundValueOperations<String, Object> key = redisTemplate.boundValueOps("redisTmp-1");
        key.set("hello 世界");
        return key.get().toString();
    }

    @GetMapping("jedis")
    public String testJedis() {
        jedis.auth("qaq123..");
        jedis.select(1);
        // set string
        jedis.set("jedis-1", "hello jedis");

        // set hash
        jedis.hset("user:1", "name", "wzq");
        jedis.hset("user:1", "age", "22");
        jedis.hset("user:1", "wm", "大帅比");

        //set list
        jedis.lpush("list-1", "l", "i", "s", "t", "1");
        jedis.lpush("list-2", "l", "i", "s", "t", "2");
        System.out.println(jedis.lrange("list-1", 0, -1));

        //set set
        jedis.sadd("set-1", "s", "e", "t");
        System.out.println(jedis.smembers("set-1"));
        return jedis.hgetAll("user:1").toString();
    }

    @GetMapping("jedisPool")
    public String testJedisPool() {
        Jedis poolJedis = jedisPool.getResource();
        poolJedis.select(2);
        // set string
        poolJedis.set("jedisPool-1", "hello jedis-pool");

        // set hash
        poolJedis.hset("pool-user:1", "name", "wzq");
        poolJedis.hset("pool-user:1", "age", "22");
        poolJedis.hset("pool-user:1", "wm", "大帅比");

        //set list
        poolJedis.lpush("pool-list-1", "l", "i", "s", "t", "1");
        poolJedis.lpush("pool-list-2", "l", "i", "s", "t", "2");
        System.out.println(poolJedis.lrange("pool-list-1", 0, -1));

        //set set
        poolJedis.sadd("pool-set-1", "s", "e", "t");
        System.out.println(poolJedis.smembers("pool-set-1"));
        return poolJedis.hgetAll("pool-user:1").toString();
    }

    @MyLogger
    @GetMapping("redisson")
    public String testRedisson() {
        String lockKey = "lock";
        RLock lock = redissonClient.getLock(lockKey);
        boolean lockResult = lock.tryLock();
        log.info("lock key {} lock result {}", lockKey, lockResult);
        ThreadUtil.sleep(5000);
        lock.unlock();
        log.info("lock key {} unlock ", lockKey);
        return System.currentTimeMillis() + "---" + lockResult;
    }



}
